package com.aier.user.action;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.ClassParent;
import com.aier.classes.entity.ClassStudent;
import com.aier.classes.entity.ClassTeacher;
import com.aier.classes.entity.Classes;
import com.aier.classes.entity.Homework;
import com.aier.classes.entity.HomeworkCR;
import com.aier.classes.entity.HomeworkComment;
import com.aier.classes.service.IClassService;
import com.aier.classes.service.IClassUserService;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.common.common.ImageUtil;
import com.aier.common.common.SendSchoolPhoneMessage;
import com.aier.common.common.SystemConfigStatic;
import com.aier.common.service.ISysPhoneMessageService;
import com.aier.proxy.service.IProxyService;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolIcCard;
import com.aier.school.entity.SchoolMessage;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.UserIcardInfo;
import com.aier.school.service.ISchoolIcCardService;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.entity.Appraise;
import com.aier.user.entity.AppraiseComment;
import com.aier.user.entity.Dialog;
import com.aier.user.entity.DialogComment;
import com.aier.user.entity.User;
import com.aier.user.service.IFindPeopleService;
import com.aier.user.service.IHsConBookService;
import com.aier.user.service.IUserBuyService;

@Scope("prototype")
@Component("useHsConBookAction")
public class UseHsConBookAction extends UserBaseAction {

    /**
     * 家校联系薄
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "hsConBookService")
    private IHsConBookService hsConBookService;
    @Resource(name = "classUserService")
    private IClassUserService classUserService;
    @Resource(name = "classService")
    private IClassService classService;
    @Resource(name = "schoolService")
    private ISchoolService schoolService;
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    @Resource(name = "findPeopleService")
    private IFindPeopleService findPeopleService;
    @Resource(name = "schoolIcCardService")
    private ISchoolIcCardService schoolIcCardService;
    @Resource(name = "sysPhoneMessageService")
    private ISysPhoneMessageService sysPhoneMessageService;
    @Resource(name = "userBuyService")
    private IUserBuyService userBuyService;

    private int s; // 错误码
    private String error_info; // 错误信息
    private int page; // 页数
    private int page_size; // 每页的记录数
    private int page_count; // 总页数
    private int count; // 总数

    private List<ClassStudent> csList; // 班级的全部学生
    private List<Dialog> dialogList; // 对话列表
    private List<Appraise> appraiseList;// 评语列表
    private List<Homework> hwList; // 作业列表
    private List<SchoolMessage> schoolMessageList;// 公告列表
    private List<Classes> classList; // 用户所在班级
    private List<SchoolTeacher> schoolTeacher;// 学校教师
    private List<User> babyList; // 家长的宝宝用户列表
    private List<ClassTeacher> ctList; // 班级的教师列表
    private List<UserIcardInfo> stuIcardInfList; // 宝宝进出园记录列表
    private List<SchoolIcCard> schoolIcCardList; // 宝宝绑定的IC卡列表
    private List<HomeworkComment> hcList; // 某条作业的评论列表
    private List<HomeworkCR> hcrList; // 某条作业评论的回复列表
    private HomeworkCR hcr; // 作业评论的某条回复
    private List<DialogComment> dcList; // 某条对话的评论列表
    private List<AppraiseComment> acList; // 某条评语的评论列表

    private Integer sid; // 学校id
    private String sname; //学校名
    private Long cid; // 班级id
    private String cname; // 班级名
    private Long buid; // 学生用户id
    private String bname; // 宝宝名字
    private Long tid; // 教师id
    private Long did; // 对话id
    private Long apid; // 评语id
    private Long hid; // 作业id
    private Long chid; // 评论id
    private String buidStr; // 多个学生用户id（以，号分隔）
    private String tidStr; // 多个教师id(以，号分隔)
    private Short type; // 身份类型（0普通用户1教师2家长3既是教师又是家长）
    private File[] image; // 图片
    private String content; // 内容
    private String imageName; // 图片名称
    private User userInfo;
    private String cardNum; // 卡号
    private String cardfoot;// 卡验证码
    private Long icid; // IC卡id
    private Short state; // 学校IC卡状态
    private Boolean isSendPhone; // 是否发送短信
    private Boolean isAllTeacher; // 是否全体教师
    private Boolean isAllClass; // 是否全体师生
    private String tidList_temp; // 教师tid列表--发送短信 选择使用
    private String cidList_temp; // 班级cid列表--发送短信 选择使用

    private String[] photoName; // 上传图片的名称或路径
    
    private SchoolTeacher st; //用户在学校的教师信息
    private ClassTeacher ct;  //班级教师信息
    private User baby;
    private String edate; // 是否付费（日期 正常使用1未购买2服务过期）
    private Integer method; // 学校付费方式
    private Integer mesCount; // 学校剩余短信数量
    
    private Long mid; //公告id
    private Long uid;//用户ID
    
    @Override
    public String execute() {
        if (sid != null) {
            schoolTeacher = schoolTeacherService.searchSchoolTeacherList(sid);
            classList = classService.searchClassList(sid);
        }
        if (cid != null) {
            csList = classUserService.searchAllClassStudentList(cid);
        }
        if (buid != null) {
            ctList = classUserService.searchTeacherByBuid(buid);
        }

        // 查询个人资料(家长用户查询相应的宝宝信息)
        if (buid != null) {
            userInfo = findPeopleService.searchSelfInfo(buid);
        } else {
            userInfo = findPeopleService.searchSelfInfo((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        }
        
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查询所有的家校联系薄（管理多个学校、班级、有多个宝宝）
     * @return
     */
    public String searchAllBook() {
        // 学校管理员老师
        schoolTeacher = hsConBookService.searchSTByUid((Long) request
                .getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        // 管理的班级
        classList = hsConBookService.searchTClasses((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        // 家长的宝宝列表(加入班级的)
        babyList = hsConBookService.searchBabyByUid((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID));
            
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 根据sid和uid查询学校教师信息
     * @return
     */
    public String searchSchoolBySidUid() {
        st = schoolTeacherService.searchSchoolTeacherByIds((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), sid);
        School school = userBuyService.searchSchoolMessageCountSid(sid);
        method = school.getMethod();
        mesCount = st.getMesCount();
        s = 1;
        return SUCCESS;
    }
    /**
     * 根据cid和tid查询学校教师信息
     * @return
     */
    public String searchTeacherByCidUid() {
        ct = classUserService.searchClassTeacherByIds((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), cid);
        School school = userBuyService.searchSchoolMessageCount(cid);
        method = school.getMethod();
        SchoolTeacher st = schoolTeacherService.searchSchoolTeacherById(tid);
        mesCount = st.getMesCount();
        s = 1;
        return SUCCESS;
    } 
    /**
     * 
     * @return
     */
    public String searchSchoolBySidUidApp() {
        st = schoolTeacherService.searchSchoolTeacherByIds(uid, sid);
        School school = userBuyService.searchSchoolMessageCountSid(sid);
        method = school.getMethod();
        mesCount = st.getMesCount();
        s = 1;
        return SUCCESS;
    }
    /**
     * 
     * @return
     */
    public String searchTeacherByCidUidApp() {
        ct = classUserService.searchClassTeacherByIds(uid, cid);
        School school = userBuyService.searchSchoolMessageCount(cid);
        method = school.getMethod();
        SchoolTeacher st = schoolTeacherService.searchSchoolTeacherById(tid);
        mesCount = st.getMesCount();
        s = 1;
        return SUCCESS;
    } 
    
    /**
     * 根据家长uid和宝宝uid查询宝宝信息
     * @return
     */
    public String searchBabyByUid() {
        baby = classUserService.searchBabyByUid((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), buid);
        if (baby != null) {
            // 查询宝宝所在学校的付费方式
            int method = userBuyService.searchSchoolBuyMethod(baby.getUid());
            // 查询有效期
            String edate = userBuyService.searchEdate((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
            
            if (method == 2 ) {
                // 未购买服务
                edate = "3";
            } else {
                if (edate == null || "".equals(edate)) {
                    // 未购买服务
                    edate = "1";
                } else {
                    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
                    String dateStr = df.format(new Date());
                    Date date = null ;
                    Date nowdate = null ;
                    try {
                        date = df.parse(edate);
                        nowdate = df.parse(dateStr);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    boolean flag = date.before(nowdate);
                    if(flag) {
                        // 服务已过期
                        edate = "2";
                    }
                }
            }
            
            baby.setEdate(edate);
        }
        
        s = 1;
        return SUCCESS;
    }

    /**
     * 获取班级的学生列表或教师列表
     * 
     * @return
     */
    public String searchSTList() {
        if (sid != null) {
            schoolTeacher = schoolTeacherService.searchSchoolTeacherList(sid);
            classList = classService.searchClassList(sid);
        }
        if (cid != null) {
            csList = classUserService.searchAllClassStudentList(cid);
        }
        if (buid != null) {
            ctList = classUserService.searchTeacherByBuid(buid);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 教师发起对话
     * 
     * @return
     */
    public String launchTDialog() {
        s = hsConBookService.insertTDialog(cid, tid, buidStr, content);
        return SUCCESS;
    }

    /**
     * 家长发起对话
     * 
     * @return
     */
    public String launchPDialog() {
        s = hsConBookService.insertPDialog(cid, tidStr, (Long) request
                .getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),
                buid, content);
        return SUCCESS;
    }

    /**
     * 回复对话
     * 
     * @return
     */
    public String commentDialog() {
        s = hsConBookService.insertCommentDialog((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), did, content,tid,
                type);
        return SUCCESS;
    }

    /**
     * 家长根据孩子查看对话记录
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchPDialogList() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = hsConBookService.searchPDialogList(page,
                page_size, buid);

        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);

            dialogList = (List<Dialog>) map.get(ICommonDAO.OBJ_LIST);

            if (null != dialogList && dialogList.size() > 0) {
                for (Dialog d : dialogList) {
                    d.setDcList(hsConBookService.searchDCByDid(d.getDid()));
                }
            }
        }
        s = 1;
        return SUCCESS;
    }

    /**
     * 教师查看对话记录
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchTDialogList() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = hsConBookService.searchTDialogList(page,
                page_size, cid, tid);

        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            dialogList = (List<Dialog>) map.get(ICommonDAO.OBJ_LIST);

            if (null != dialogList && dialogList.size() > 0) {
                for (Dialog d : dialogList) {
                    d.setDcList(hsConBookService.searchDCByDid(d.getDid()));
                }
            }
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查询对话的评论列表
     * @return
     */
    public String searchDCList() {
        dcList = hsConBookService.searchDCByDid(did);
        s = 1;
        return SUCCESS;
    }

    /**
     * 教师写评语
     * 
     * @return
     */
    public String launchAppraise() {
        s = 1;
        for (String bstr:buidStr.split(",")) {
            Long aid = hsConBookService.insertAppraise(cid, tid, bstr, content);
            // 是否发送短信
            if (isSendPhone) {
                List<List<String>> userPhoneList = new ArrayList<List<String>>();
                List<User> uList = classUserService.searchParentByBuid(Long
                        .valueOf(bstr));
                for (User u : uList) {
                    List<String> list = new ArrayList<String>();
                    list.add(u.getPhone());
                    list.add(u.getUid().toString());
                    list.add("0");
                    userPhoneList.add(list);
                }
                
                // 查询学校剩余短信数
                School school = userBuyService.searchSchoolMessageCount(cid);
                if (userPhoneList.size() > 0) {
                    Classes cl = classService.searchClassById(cid);
                    SchoolTeacher st = schoolTeacherService
                            .searchSchoolTeacherById(tid);
                    String mesSuffix = school.getMesSuffix();
                    // 判断评语的长度,计算每条评语需要发送几条短信(在发送长短信的时候，超过70个字的按67个字一条计算)
                    content = InviteRequestUtil.getFilterMessage(content);
                    if (null != mesSuffix && mesSuffix.length() > 0) {
                        content = cl.getCname() + "的" + st.getTname() + "老师评价了您的宝宝："
                                + content + "【" + mesSuffix + "】";
                    } else {
                        content = cl.getCname() + "的" + st.getTname() + "老师评价了您的宝宝："
                                + content;
                    }
                    
                    int size = 1;
                    
                    if (content.length() + Constant.num_7 > Constant.num_70) {
                        int len = content.length()+Constant.num_7;
                        size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
                    }
                    
                    if (school.getMethod() == 2 && st.getMesCount() < userPhoneList.size()*size) {
                        s = 2;
                    } else {
                        new SendSchoolPhoneMessage(userPhoneList, content, cl.getSid(),
                                sysPhoneMessageService,Constant.num_4,aid,size,tid).start();
                    }
                }
            }
            
        }
        mesCount = schoolTeacherService.searchSchoolTeacherById(tid).getMesCount();
        return SUCCESS;
        
    }

    /**
     * 回复评语
     * 
     * @return
     */
    public String commentAppraise() {
        s = hsConBookService.insertCommentAppraise((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), apid, content,tid,
                type);
        return SUCCESS;
    }

    /**
     * 家长查看评语记录
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchPAppraiseList() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        appraiseList = new ArrayList<Appraise>();
        Map<String, Object> map = hsConBookService.searchPAppraiseList(page,
                page_size, buid);

        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            appraiseList = (List<Appraise>) map.get(ICommonDAO.OBJ_LIST);

            if (null != appraiseList && appraiseList.size() > 0) {
                for (Appraise a : appraiseList) {
                    a.setAcList(hsConBookService.searchACByDid(a.getApid()));
                }
            }
        }
        s = 1;
        return SUCCESS;
    }

    /**
     * 教师查看评语记录
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchTAppraiseList() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = hsConBookService.searchTAppraiseList(page,
                page_size, cid, tid);

        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            appraiseList = (List<Appraise>) map.get(ICommonDAO.OBJ_LIST);

            if (null != appraiseList && appraiseList.size() > 0) {
                for (Appraise a : appraiseList) {
                    a.setAcList(hsConBookService.searchACByDid(a.getApid()));
                }
            }
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查询评语的评论列表
     * @return
     */
    public String searchACList() {
        acList = hsConBookService.searchACByDid(apid);
        s = 1;
        return SUCCESS;
    }

    /**
     * 教师发布作业
     * 
     * @return
     */
    public String commitHomework() {
        s = 1;
        List<ClassStudent> ctList = classUserService.searchAllClassStudentList(cid);
        Long hid = hsConBookService.insertHomework(cid, tid, null, content, imageName,ctList.size());
        // 是否发送短信
        if (isSendPhone) {
            List<List<String>> userPhoneList = new ArrayList<List<String>>();

            List<User> uList = classUserService.searchAllClassParentList(cid);
            for (User u : uList) {
                List<String> list = new ArrayList<String>();
                list.add(u.getPhone());
                list.add(u.getUid().toString());
                list.add("0");
                userPhoneList.add(list);
            }
            // 查询学校剩余短信数
            School school = userBuyService.searchSchoolMessageCount(cid);
            if (userPhoneList.size() > 0) {
                
                Classes cl = classService.searchClassById(cid);
                SchoolTeacher st = schoolTeacherService
                        .searchSchoolTeacherById(tid);
                String mesSuffix = school.getMesSuffix();
                
                // 判断作业的长度,计算每条评语需要发送几条短信
                content = InviteRequestUtil.getFilterMessage(content);
                if (null != mesSuffix && mesSuffix.length() > 0) {
                    content = cl.getCname() + "的" + st.getTname() + "老师布置了作业："
                            + content + "【" + mesSuffix + "】";
                } else {
                    content = cl.getCname() + "的" + st.getTname() + "老师布置了作业："
                            + content;
                }
                int size = 1;
                
                if (content.length() + Constant.num_7 > Constant.num_70) {
                    int len = content.length()+Constant.num_7;
                    size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
                }
                
                if (school.getMethod() == 2 && st.getMesCount() < userPhoneList.size()*size) {
                    s = 2;
                } else {
                    new SendSchoolPhoneMessage(userPhoneList, content, cl.getSid(),
                            sysPhoneMessageService,Constant.num_3,hid,size,tid).start();
                }
                
            }
            
        }
        mesCount = schoolTeacherService.searchSchoolTeacherById(tid).getMesCount();
        return SUCCESS;
    }

    /**
     * 评论作业或晒作业
     * 
     * @return
     */
    public String commentHomework() {
        hsConBookService.insertHComment((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), hid, content,
                imageName, type, buid,tid);
        s = 1;
        return SUCCESS;
    }

    /**
     * 回复评论
     * 
     * @return
     */
    public String replyComment() {
        hcr = hsConBookService.insertReply((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), chid, content,
                type, buid,tid);
        s = 1;
        return SUCCESS;
    }

    /**
     * 家长获取作业列表
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchPHomework() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = hsConBookService.searchPHomework(page,
                page_size, buid);

        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            hwList = (List<Homework>) map.get(ICommonDAO.OBJ_LIST);

            if (null != hwList && hwList.size() > 0) {
                for (Homework h : hwList) {
                    h.setHcList(hsConBookService.searchHCList(h.getHid()));
                    for (int i = 0; i < h.getHcList().size(); i++) {
                        h.getHcList()
                                .get(i)
                                .setHcrList(
                                        hsConBookService.searchHCRList(h
                                                .getHcList().get(i).getChid()));
                    }
                }
            }
        }
        s = 1;
        return SUCCESS;
    }

    /**
     * 教师查询作业列表
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchTHomework() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = hsConBookService.searchTHomework(page,
                page_size, cid);

        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            hwList = (List<Homework>) map.get(ICommonDAO.OBJ_LIST);

            if (null != hwList && hwList.size() > 0) {
                for (Homework h : hwList) {
                    h.setHcList(hsConBookService.searchHCList(h.getHid()));
                    for (int i = 0; i < h.getHcList().size(); i++) {
                        h.getHcList()
                                .get(i)
                                .setHcrList(
                                        hsConBookService.searchHCRList(h
                                                .getHcList().get(i).getChid()));
                    }
                }
            }
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取作业的评论列表
     * @return
     */
    public String searchHWComment() {
        hcList = hsConBookService.searchHCList(hid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取某条作业评论的回复
     * @return
     */
    public String searchHWCReply() {
        hcrList = hsConBookService.searchHCRList(chid);
        s = 1;
        return SUCCESS;
    }

    /**
     * 查询公告
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchSchoolMessageList() {
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        try {
            Map<String, Object> map = null;
            if (cid == null) {
                map = schoolService.searchSchoolMessageList(page, page_size,
                        sid);
            } else {
                map = schoolService
                        .searchClassMessageList(page, page_size, cid);
            }
            if (null != map) {
                schoolMessageList = (List<SchoolMessage>) map
                        .get(ICommonDAO.OBJ_LIST);
                s = 1;
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
                page = (Integer) map.get(ICommonDAO.PAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    // 发布公告前提示发送短信条数
    public String searchMessageCount() {
        if (isSendPhone) {
            List<SchoolTeacher> stList = new ArrayList<SchoolTeacher>();
            List<ClassParent> cpList = new ArrayList<ClassParent>();
            if (isAllTeacher) {// 全体教师
                stList = schoolTeacherService
                        .searchSchoolTeacherListSid_account(sid);
            } else if (tidList_temp != null
                    && !"".equals(tidList_temp.trim())) {// 选择部分教师
                String[] tidList_str = tidList_temp.split(",");
                List<Long> tidList = new ArrayList<Long>();
                for (String tid_tmp : tidList_str) {
                    tidList.add(Long.valueOf(tid_tmp));
                }
                stList = schoolTeacherService
                        .searchSchoolTeacherListTidList_account(tidList);
            }
            if (isAllClass) {// 全体师生
                cpList = classUserService
                        .searchClassParentListSid_account(sid);
                stList = schoolTeacherService
                        .searchSchoolTeacherListSid_account(sid);
            } else if (cidList_temp != null
                    && !"".equals(cidList_temp.trim())) {// 部分班级的师生
                String[] cidList_str = cidList_temp.split(",");
                List<Long> cidList = new ArrayList<Long>();
                for (String cid_tmp : cidList_str) {
                    cidList.add(Long.valueOf(cid_tmp));
                }
                cpList = classUserService
                        .searchClassParentListCidList_account(cidList);
                stList = schoolTeacherService
                        .searchSchoolTeacherListCidList_account(cidList);
            }
            
            School school = userBuyService.searchSchoolMessageCountSid(sid);
            String mesSuffix = school.getMesSuffix();
            SchoolTeacher st = schoolTeacherService.searchSchoolTeacherById(tid);
            // 判断公告的长度,计算每条评语需要发送几条短信
            content = InviteRequestUtil.getFilterMessage(content);
            if (null != mesSuffix && mesSuffix.length() > 0) {
                content = "学校公告:"+ content +" --"+st.getTname()+"老师" + "【" + mesSuffix + "】";
            } else {
                content = "学校公告:"+ content +" --"+st.getTname()+"老师";
            }
            
            int size = 1;
            
            if (content.length() + Constant.num_7 > Constant.num_70) {
                int len = content.length()+Constant.num_7;
                size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
            }
            
            mesCount = (cpList.size() + stList.size())*size;
            
        }
        s = 1;
        return SUCCESS;
    }
    
    // 发布公告
    public String addSchoolMessage() {
        List<List<String>> userPhoneList = new ArrayList<List<String>>();
        List<SchoolTeacher> stList = new ArrayList<SchoolTeacher>();
        List<ClassParent> cpList = new ArrayList<ClassParent>();
        s = 1;
        if (cid == null) {
            SchoolTeacher st = schoolTeacherService.searchSchoolTeacherById(tid);
            if (st.getIsadmin() == (short) 1) {
                Long mid = schoolService.insertSchoolMessage(sid, tid, content,imageName);
                if (isSendPhone) {// 是否发送短信
                    if (isAllTeacher) {// 全体教师
                        stList = schoolTeacherService
                                .searchSchoolTeacherListSid_account(sid);
                    } else if (tidList_temp != null
                            && !"".equals(tidList_temp.trim())) {// 选择部分教师
                        String[] tidList_str = tidList_temp.split(",");
                        List<Long> tidList = new ArrayList<Long>();
                        for (String tid_tmp : tidList_str) {
                            tidList.add(Long.valueOf(tid_tmp));
                        }
                        stList = schoolTeacherService
                                .searchSchoolTeacherListTidList_account(tidList);
                    }
                    if (isAllClass) {// 全体师生
                        cpList = classUserService
                                .searchClassParentListSid_account(sid);
                        stList = schoolTeacherService
                                .searchSchoolTeacherListSid_account(sid);
                    } else if (cidList_temp != null
                            && !"".equals(cidList_temp.trim())) {// 部分班级的师生
                        String[] cidList_str = cidList_temp.split(",");
                        List<Long> cidList = new ArrayList<Long>();
                        for (String cid_tmp : cidList_str) {
                            cidList.add(Long.valueOf(cid_tmp));
                        }
                        cpList = classUserService
                                .searchClassParentListCidList_account(cidList);
                        stList = schoolTeacherService
                                .searchSchoolTeacherListCidList_account(cidList);
                    }
                    for (SchoolTeacher sTeacher : stList) {
                        List<String> upList = new ArrayList<String>();
                        upList.add(sTeacher.getAccount());
                        upList.add(sTeacher.getUid().toString());
                        upList.add("1");
                        userPhoneList.add(upList);
                    }
                    for (ClassParent sParent : cpList) {
                        List<String> upList = new ArrayList<String>();
                        upList.add(sParent.getAccount());
                        upList.add(sParent.getUid().toString());
                        upList.add("0");
                        userPhoneList.add(upList);
                    }
                    
                    // 查询学校剩余短信数
                    School school = userBuyService.searchSchoolMessageCountSid(sid);
                    if (userPhoneList.size() > 0) {
                        
                        String mesSuffix = school.getMesSuffix();
                        
                        // 判断公告的长度,计算每条评语需要发送几条短信
                        content = InviteRequestUtil.getFilterMessage(content);
                        if (null != mesSuffix && mesSuffix.length() > 0) {
                            content = "学校公告:"+ content +" --"+st.getTname()+"老师" + "【" + mesSuffix + "】";
                        } else {
                            content = "学校公告:"+ content +" --"+st.getTname()+"老师";
                        }
                        int size = 1;
                        if (content.length() + Constant.num_7 > Constant.num_70) {
                            int len = content.length()+Constant.num_7;
                            size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
                        }
                        
                        if (school.getMethod() == 2 && st.getMesCount() < userPhoneList.size()*size) {
                            s = 2;
                        } else {
                            new SendSchoolPhoneMessage(userPhoneList, content, sid,
                                    sysPhoneMessageService,1,mid,size,tid).start();
                            // 查询短信成功发送条数和失败的用户
//                            susCount = hsConBookService.searchMessageSuccessCount(mid);
//                            failStr = hsConBookService.searchMessageFailStr(mid);
                        }
                    }
                    
                }

            } else {
                s = 0;
                error_info = "您不是学校管理员,无权发布公告";
            }
        } else {
            Long mid = schoolService.insertClassMessage(cid, tid, content, imageName);
            if (isSendPhone) {// 是否发送短信
                SchoolTeacher st = schoolTeacherService.searchSchoolTeacherById(tid);
                List<Long> cidList = new ArrayList<Long>();
                cidList.add(cid);
                cpList = classUserService
                        .searchClassParentListCidList_account(cidList);
                stList = schoolTeacherService
                        .searchSchoolTeacherListCidList_account(cidList);
                for (SchoolTeacher sTeacher : stList) {
                    List<String> upList = new ArrayList<String>();
                    upList.add(sTeacher.getAccount());
                    upList.add(sTeacher.getUid().toString());
                    upList.add("1");
                    userPhoneList.add(upList);
                }
                for (ClassParent sParent : cpList) {
                    List<String> upList = new ArrayList<String>();
                    upList.add(sParent.getAccount());
                    upList.add(sParent.getUid().toString());
                    upList.add("0");
                    userPhoneList.add(upList);
                }
                
                // 查询学校剩余短信数
                School school = userBuyService.searchSchoolMessageCount(cid);
                if (userPhoneList.size() > 0) {
                    
                    String mesSuffix = school.getMesSuffix();
                    
                    // 判断公告的长度,计算每条评语需要发送几条短信
                    content = InviteRequestUtil.getFilterMessage(content);
                    if (null != mesSuffix && mesSuffix.length() > 0) {
                        content = "班级公告:"+ content +" --"+st.getTname()+"老师" + "【" + mesSuffix + "】";
                    } else {
                        content = "班级公告:"+ content +" --"+st.getTname()+"老师";
                    }
                    int size = 1;
                    if (content.length() + Constant.num_7 > Constant.num_70) {
                        int len = content.length()+Constant.num_7;
                        size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
                    }
                    
                    if (school.getMethod() == 2 && st.getMesCount() < userPhoneList.size()*size) {
                        s = 2;
                    } else {
                        new SendSchoolPhoneMessage(userPhoneList, content, sid,
                                sysPhoneMessageService,1,mid,size,tid).start();
                    }
                    
                }

            }
        }
        mesCount = schoolTeacherService.searchSchoolTeacherById(tid).getMesCount();
        return SUCCESS;

    }

    /**
     * 查询宝宝进出园记录(家长身份)
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchStuIcardInfoList_parent() {
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        try {
            Map<String, Object> map = schoolIcCardService
                    .searchUserIcardInfoListByCidABuid(page,
                            IProxyService.PROXY_CARD_LIST_PAGESIZE, buid);
            if (null != map) {
                stuIcardInfList = (List<UserIcardInfo>) map
                        .get(ICommonDAO.OBJ_LIST);
                if (null != stuIcardInfList) {
                    s = 1;
                }
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
                page = (Integer) map.get(ICommonDAO.PAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;

    }

    /**
     * 查询宝宝进出园记录(教师身份)
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchStuIcardInfoList_teacher() {
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        try {
            if (cid != null) {
                Map<String, Object> map = schoolIcCardService
                        .searchUserIcardInfoListByCondation(
                                page,
                                SchoolFinalPageSize.ATTEND_DETAIL_LIST_PAGESIZE,
                                null, cid, null, null,null);
                if (null != map) {
                    stuIcardInfList = (List<UserIcardInfo>) map
                            .get(ICommonDAO.OBJ_LIST);
                    if (null != stuIcardInfList) {
                        s = 1;
                    }
                    page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                    count = (Integer) map.get(ICommonDAO.COUNT);
                    page = (Integer) map.get(ICommonDAO.PAGE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;

    }

    // 查询宝宝的ic卡列表
    /**
     * update by zhou 2014-05-24
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchStuSchoolIcCardList() {
        
        if (page <= 0) {
            page = 1;
        }
        try {
            Map<String, Object> map = schoolIcCardService
                    .searchSchoolCardsByCidABuid(page,
                            page_size, cid, buid);
            if (null != map) {
                schoolIcCardList = (List<SchoolIcCard>) map
                        .get(ICommonDAO.OBJ_LIST);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
                page = (Integer) map.get(ICommonDAO.PAGE);
            }
            s = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    // 宝宝添加IC卡绑定
    public String addStudentIcard() {
        sid = schoolService.searchSchoolByCid(cid).getSid();
        SchoolIcCard scard = schoolIcCardService.searchSchoolIcCard(cardNum,
                cardfoot, sid);
        if (scard == null) {
            s = 0;
            error_info = "卡号验证码有误 或 该卡不属于所在学校";
        } else {
            int num = schoolIcCardService.searchBandCard(sid, scard.getIcid());
            if (num > 0) {
                s = 0;
                error_info = "该卡已经被绑定";
            } else {
                ClassStudent cs = classUserService
                        .searchClassStudentByBuid(buid);
                s = schoolIcCardService.updateBindCardStudent(sid,
                        scard.getIcid(), cs.getCsid());
            }
        }
        return SUCCESS;
    }

    // 修改ic卡状态
    public String updateSchoolIcardState() {
        s = schoolIcCardService.updateIcardState(icid, state);
        return SUCCESS;
    }

    /**
     * 客户端教师发布作业
     * 
     * @return
     */
    public String commitHomeworkApp() {
        s = 1;
        String picName = "";
        if (null != image) {
            for (int i = 0; i < image.length; i++) {
                File file = image[i];

                String fileName = ImageUtil.uploadImage(file,
                        SystemConfigStatic.homework_upload_url, photoName[i],1);

                if (!"".equals(picName)) {
                    picName += ",";
                }
                picName += fileName;
            }
        }
        List<ClassStudent> ctList = classUserService.searchAllClassStudentList(cid);
        Long hid = hsConBookService.insertHomework(cid, tid, null, content, picName,ctList.size());
        if (isSendPhone == null) {
            isSendPhone = false;
        }
        // 是否发送短信
        if (isSendPhone) {
            List<List<String>> userPhoneList = new ArrayList<List<String>>();

            List<User> uList = classUserService.searchAllClassParentList(cid);
            for (User u : uList) {
                List<String> list = new ArrayList<String>();
                list.add(u.getPhone());
                list.add(u.getUid().toString());
                list.add("0");
                userPhoneList.add(list);
            }
            // 查询学校剩余短信数
            School school = userBuyService.searchSchoolMessageCount(cid);
            if (userPhoneList.size() > 0) {
                Classes cl = classService.searchClassById(cid);
                SchoolTeacher st = schoolTeacherService
                        .searchSchoolTeacherById(tid);
                String mesSuffix = school.getMesSuffix();
                
                // 判断作业的长度,计算每条评语需要发送几条短信
                content = InviteRequestUtil.getFilterMessage(content);
                if (null != mesSuffix && mesSuffix.length() > 0) {
                    content = cl.getCname() + "的" + st.getTname() + "老师布置了作业："
                            + content + "【" + mesSuffix + "】";
                } else {
                    content = cl.getCname() + "的" + st.getTname() + "老师布置了作业：" + content;
                }
                
                int size = 1;
                if (content.length() + Constant.num_7 > Constant.num_70) {
                    int len = content.length()+Constant.num_7;
                    size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
                }
                
                if (school.getMethod() == 2 && st.getMesCount() < userPhoneList.size()*size) {
                    s = 2;
                } else {
                    new SendSchoolPhoneMessage(userPhoneList, content, cl.getSid(),
                            sysPhoneMessageService,Constant.num_3,hid,size,tid).start();
                }
            }

        }
        mesCount = schoolTeacherService.searchSchoolTeacherById(tid).getMesCount();
        
        return SUCCESS;
    }

    /**
     * 客户端评论作业或晒作业
     * 
     * @return
     */
    public String commentHomeworkApp() {
        String picName = "";
        if (null != image) {
            for (int i = 0; i < image.length; i++) {
                File file = image[i];

                String fileName = ImageUtil
                        .uploadImage(file,
                                SystemConfigStatic.homework_upload_url,
                                photoName[i], 1);

                if (!"".equals(picName)) {
                    picName += ",";
                }
                picName += fileName;
            }
        }
        hsConBookService.insertHComment((Long) request.getSession()
                .getAttribute(CommonSession.SESSION_ACCOUNT_ID), hid, content,
                picName, type, buid,tid);
        s = 1;
        return SUCCESS;
    }

    // 客户端发布公告
    public String addSchoolMessage_app() {
        String picName = "";
        if (null != image) {
            for (int i = 0; i < image.length; i++) {
                File file = image[i];

                String fileName = ImageUtil.uploadImage(file,
                        SystemConfigStatic.homework_upload_url, photoName[i],
                        (int) 1);

                if (!"".equals(picName)) {
                    picName += ",";
                }
                picName += fileName;
            }
        }
        if (cid == null) {
            SchoolTeacher st = schoolTeacherService
                    .searchSchoolTeacherById(tid);
            if (st.getIsadmin() == (short) 1) {
                schoolService.insertSchoolMessage(st.getSid(), tid,
                        content, picName);
            } else {
                s = 0;
                error_info = "您不是学校管理员,无权发布公告";
            }
        } else {
            schoolService.insertClassMessage(cid, tid, content, picName);
        }
        return SUCCESS;

    }
    
    /**
     * 删除公告
     * @return
     */
    public String deleteMessage() {
        s = hsConBookService.deleteMessage(mid);
        return SUCCESS;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getS() {
        return s;
    }

    public void setHsConBookService(IHsConBookService hsConBookService) {
        this.hsConBookService = hsConBookService;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setBuid(Long buid) {
        this.buid = buid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setBuidStr(String buidStr) {
        this.buidStr = buidStr;
    }

    public void setTidStr(String tidStr) {
        this.tidStr = tidStr;
    }

    public void setDid(Long did) {
        this.did = did;
    }

    public List<Dialog> getDialogList() {
        return dialogList;
    }

    public void setType(Short type) {
        this.type = type;
    }

    public List<Appraise> getAppraiseList() {
        return appraiseList;
    }

    public void setApid(Long apid) {
        this.apid = apid;
    }

    public void setClassUserService(IClassUserService classUserService) {
        this.classUserService = classUserService;
    }

    public List<ClassStudent> getCsList() {
        return csList;
    }

    public void setImage(File[] image) {
        this.image = image;
    }

    public void setHid(Long hid) {
        this.hid = hid;
    }

    public void setChid(Long chid) {
        this.chid = chid;
    }

    public List<Homework> getHwList() {
        return hwList;
    }

    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public List<Classes> getClassList() {
        return classList;
    }

    public Long getTid() {
        return tid;
    }

    public Long getCid() {
        return cid;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public Short getType() {
        return type;
    }

    public List<SchoolTeacher> getSchoolTeacher() {
        return schoolTeacher;
    }

    public List<User> getBabyList() {
        return babyList;
    }

    public void setFindPeopleService(IFindPeopleService findPeopleService) {
        this.findPeopleService = findPeopleService;
    }

    public User getUserInfo() {
        return userInfo;
    }

    public List<ClassTeacher> getCtList() {
        return ctList;
    }

    public Long getBuid() {
        return buid;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public List<SchoolMessage> getSchoolMessageList() {
        return schoolMessageList;
    }

    public List<UserIcardInfo> getStuIcardInfList() {
        return stuIcardInfList;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

    public void setCardfoot(String cardfoot) {
        this.cardfoot = cardfoot;
    }

    public String getError_info() {
        return error_info;
    }

    public void setIcid(Long icid) {
        this.icid = icid;
    }

    public void setState(Short state) {
        this.state = state;
    }

    public List<SchoolIcCard> getSchoolIcCardList() {
        return schoolIcCardList;
    }

    public void setSchoolTeacherService(
            ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }

    public int getPage() {
        return page;
    }

    public void setPhotoName(String[] photoName) {
        this.photoName = photoName;
    }

    public void setSysPhoneMessageService(
            ISysPhoneMessageService sysPhoneMessageService) {
        this.sysPhoneMessageService = sysPhoneMessageService;
    }

    public void setIsSendPhone(Boolean isSendPhone) {
        this.isSendPhone = isSendPhone;
    }

    public void setIsAllClass(Boolean isAllClass) {
        this.isAllClass = isAllClass;
    }

    public void setIsAllTeacher(Boolean isAllTeacher) {
        this.isAllTeacher = isAllTeacher;
    }

    public void setTidList_temp(String tidList_temp) {
        this.tidList_temp = tidList_temp;
    }

    public void setCidList_temp(String cidList_temp) {
        this.cidList_temp = cidList_temp;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public SchoolTeacher getSt() {
        return st;
    }

    public ClassTeacher getCt() {
        return ct;
    }

    public User getBaby() {
        return baby;
    }

    public void setUserBuyService(IUserBuyService userBuyService) {
        this.userBuyService = userBuyService;
    }

    public List<HomeworkComment> getHcList() {
        return hcList;
    }

    public List<DialogComment> getDcList() {
        return dcList;
    }

    public List<AppraiseComment> getAcList() {
        return acList;
    }

    public String getEdate() {
        return edate;
    }

    public void setEdate(String edate) {
        this.edate = edate;
    }

    public List<HomeworkCR> getHcrList() {
        return hcrList;
    }

    public HomeworkCR getHcr() {
        return hcr;
    }

    public Integer getMesCount() {
        return mesCount;
    }

    public Integer getMethod() {
        return method;
    }

    public void setMid(Long mid) {
        this.mid = mid;
    }

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}
}
